package hui.com.controller.role;

import hui.com.model.entity.PermsEntity;
import hui.com.model.entity.RoleEntity;
import hui.com.model.req.role.RequestAddRolePerms;
import hui.com.model.req.role.RequestNOTINRolePerms;
import hui.com.model.req.users.RequestGetUserRoleAndPerms;
import hui.com.model.res.MyResponse;
import hui.com.model.res.users.ResponseGetUserRoleAndPerms;
import hui.com.service.RoleService;
import hui.com.service.RoleService;
import hui.com.service.UserLogService;
import hui.com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*")//处理跨源资源共享
@RequestMapping("/public/role")
@Controller
public class role {
    @Resource
    RoleService roleService;
    @Resource
    UserLogService userLogService;

    /**
     * 模糊查询角色
     */
    @RequestMapping("/getRoleList")
    @ResponseBody
    public Object getRoleList(@RequestBody(required = false) RoleEntity role){
        if (role==null){
            role = new RoleEntity();
        }
        return MyResponse.builder().data(roleService.getRoleList(role)).code(200).build();
    }

    /**
     * 指定角色新增权限单个/多个
     */
    @RequestMapping("/addRolePerms")
    @ResponseBody
    public Object addRolePerms(@RequestBody RequestAddRolePerms rolePerms) {
        if(roleService.addRolePerms(rolePerms.getRoleID(), rolePerms.getPermsIDArray())){
            Integer[] roleIDArray = roleService.getRoleUser(rolePerms.getRoleID());
            for(Integer UID : roleIDArray){
                userLogService.updUIDUserPermission(UID);
            }
            return MyResponse.builder().data(true).code(200).build();
        }else{
            return MyResponse.builder().data(false).code(200).build();
        }

    }

    /**
     * 删除权限
     */
    @RequestMapping("/deleteRolePerms")
    @ResponseBody
    public Object deleteRolePerms(@RequestBody Map<String, Integer> map) {
        Integer roleID = map.get("roleID");
        Integer permsID = map.get("permsID");
        if(roleID==null||permsID==null){
            return MyResponse.builder().msg("失败,传参有误"+map).code(202).build();
        }
        if(roleService.deleteRolePerms(roleID, permsID)){
            Integer[] roleIDArray = roleService.getRoleUser(roleID);
            for(Integer UID : roleIDArray){
                userLogService.updUIDUserPermission(UID);
            }
            return MyResponse.builder().data(true).code(200).build();
        }else{
            return MyResponse.builder().data(false).code(200).build();
        }
    }

    /**
     * 查询该角色没有的权限
     */
    @RequestMapping("/getNOTINRolePerms")
    @ResponseBody
    public Object getNOTINRolePerms(@RequestBody RequestNOTINRolePerms role) {
        if (role==null || role.getRoleID()==null){
            return MyResponse.builder().msg("失败,传参有误"+role).code(202).build();
        }
        if (role.getPermsName()==null){
            role.setPermsName("");
        }
        return MyResponse.builder().data(roleService.getNOTINRolePerms(role)).code(200).build();
    }


    @Resource
    UserService userService;

    /**
     * 获取用户的身份和角色
     */

    @RequestMapping("/getUserRoleAndPerms")
    @ResponseBody
    public Object getUserRoleAndPerms(@RequestBody(required = false) RequestGetUserRoleAndPerms userRoleAndPerms) {
        if(userRoleAndPerms==null){
            userRoleAndPerms = new RequestGetUserRoleAndPerms();
        }

        ResponseGetUserRoleAndPerms res =new ResponseGetUserRoleAndPerms();
        res.setUsers(userService.getUserRoleAndPerms(userRoleAndPerms));
        res.setPageMax(userService.getUserRoleAndPermsPage(userRoleAndPerms));
        return MyResponse.builder().data(res).code(200).build();
    }
}
